<template>
	<!-- 安全中心 -->
	<view>
		<view class="item" @click="navTo('/pages/safe/updateLoginPwd')">
			<view>修改登录密码</view>
			<u-icon name="arrow-right" color="#909090" size="20"></u-icon>
		</view>
		<!-- <view class="item" @click="navTo('/pages/safe/changePhone')">
			<view>更换手机号</view>
			<view>
				<u-icon name="arrow-right" color="#909090" size="20"></u-icon>
			</view>
		</view> -->
		<view class="item" @click="navTo('/pages/safe/setpwd')" v-if="isPay==false">
			<view>设置支付密码</view>
			<u-icon name="arrow-right" color="#909090" size="20"></u-icon>
		</view>
		<view class="item" @click="navTo('/pages/safe/updatePayPwd')" v-if="isPay==true">
			<view>修改支付密码</view>
			<u-icon name="arrow-right" color="#909090" size="20"></u-icon>
		</view>
		<view class="item" v-if='zhiwen==true'>
			<view>打开指纹登录</view>
			<u-switch v-model="checked" active-color="#F09238" inactive-color="#999" @change="changeZw"></u-switch>
		</view>
		<view class="item" v-if='zhiwen==true'>
			<view>打开指纹支付</view>
			<u-switch v-model="checkedpay" active-color="#F09238" inactive-color="#999" @change="changePay"></u-switch>
		</view>
		<!-- <view class="item" @click="outLogin">
			<view>退出登录</view>
			<u-icon name="arrow-right" color="#909090" size="20"></u-icon>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isPay: null,
				checked: false,
				checkedpay:false,
				zhiwen: false, //是否显示指纹登录
				isEnrolled: false //是否录入过指纹
			}
		},
		onLoad() {
			// 检查手机支持的生物认证方式
			if (uni.getStorageSync("iszhiwen")) {
				this.zhiwen = true
			}
			// 检查手机是否录入过指纹
			if (uni.getStorageSync('isEnrolled')) {
				this.isEnrolled = true
			}
		},
		onShow() {
			this.isPay = uni.getStorageSync("user").isPay
			if (uni.getStorageSync("openzw") && uni.getStorageSync("openzw") == true) {
				this.checked = true
			}
			if (uni.getStorageSync("openpay") && uni.getStorageSync("openpay") == true) {
				this.checkedpay = true
			}
		},
		methods: {
			changeZw(e) {
				console.log(e)
				if (e == true) {
					if (this.isEnrolled == true) {
						// 开始指纹识别
						uni.startSoterAuthentication({
							requestAuthModes: ['fingerPrint'],
							challenge: '123456',
							authContent: '请验证本机指纹',
							success:(res)=> {
								console.log('成功',res);
								if(res.errCode==0){
									this.msg('指纹登录已打开')
									uni.setStorageSync("openzw",true)
								}
							},
							fail:(res)=> {
								this.checked=false
								console.log('失败',res);
								if(res.errCode==90001){
									this.msg('本设备不支持生物认证','none')
								}
								if(res.errCode==90002){
									this.msg('用户未授权使用该生物认证接口','none')
								}
								if(res.errCode==90003){
									this.msg('请求使用的生物认证方式不支持','none')
								}
								if(res.errCode==90007){
									this.msg('内部错误','none')
								}
								if(res.errCode==90008){
									this.msg('用户取消授权','none')
								}
								if(res.errCode==90009){
									this.msg('识别失败','none')
								}
								if(res.errCode==90010){
									this.msg('重试次数过多被冻结','none')
								}
								if(res.errCode==90011){
									this.msg('用户未录入所选识别方式','none')
								}
							},
							complete(res) {
								console.log('结束',res);
							}
						})
					} else {
						this.checked=false
						this.showMd('当前手机未录入指纹,请先在系统录入指纹', () => {})
					}
				}else{
					this.msg('指纹登录功能已关闭','none')
					uni.setStorageSync("openzw",false)
				}
			},
			changePay(e){
				console.log(e)
				if (e == true) {
					if (this.isEnrolled == true) {
						// 开始指纹识别
						uni.startSoterAuthentication({
							requestAuthModes: ['fingerPrint'],
							challenge: '123456',
							authContent: '请验证本机指纹',
							success:(res)=> {
								console.log('成功',res);
								if(res.errCode==0){
									this.msg('指纹支付已打开')
									uni.setStorageSync("openpay",true)
								}
							},
							fail:(res)=> {
								this.checkedpay=false
								console.log('失败',res);
								if(res.errCode==90001){
									this.msg('本设备不支持生物认证','none')
								}
								if(res.errCode==90002){
									this.msg('用户未授权使用该生物认证接口','none')
								}
								if(res.errCode==90003){
									this.msg('请求使用的生物认证方式不支持','none')
								}
								if(res.errCode==90007){
									this.msg('内部错误','none')
								}
								if(res.errCode==90008){
									this.msg('用户取消授权','none')
								}
								if(res.errCode==90009){
									this.msg('识别失败','none')
								}
								if(res.errCode==90010){
									this.msg('重试次数过多被冻结','none')
								}
								if(res.errCode==90011){
									this.msg('用户未录入所选识别方式','none')
								}
							},
							complete(res) {
								console.log('结束',res);
							}
						})
					} else {
						this.checkedpay=false
						this.showMd('当前手机未录入指纹,请先在系统录入指纹', () => {})
					}
				}else{
					this.msg('指纹支付功能已关闭','none')
					uni.setStorageSync("openpay",false)
				}
			},
			// 统一跳转
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			outLogin() {
				this.showMd("确认退出登录", () => {
					uni.removeStorageSync("userInfo")
					uni.removeStorageSync("user")
					uni.switchTab({
						url: "/pages/index/index"
					})
				})
			}
		}
	}
</script>

<style>
	.item {
		width: 750rpx;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		border-bottom: 1px solid #ECECEC;
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
	}

	.phone {
		font-size: 28rpx;
		color: #F09238;
		margin-right: 10rpx;
		font-weight: 400;
	}
</style>
